package com.oracle.oa.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.CalendarBiz;
import com.oracle.oa.biz.DepartInfoBiz;
import com.oracle.oa.biz.OperatelogBiz;
import com.oracle.oa.biz.ScheduleBiz;
import com.oracle.oa.biz.impl.OperatelogBizImpl;
import com.oracle.oa.entity.Calendar;
import com.oracle.oa.entity.Departinfo;
import com.oracle.oa.entity.Schedule;
import com.oracle.oa.entity.Userinfo;

public class CalendarAction extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware {
	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
	HttpServletResponse response;
	Map<String, Object> session;
	private CalendarBiz calbiz;
	private DepartInfoBiz dbiz;
	private Calendar calendar;
	private ScheduleBiz sbiz;
	 
	private String result;
	private int id;
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	/**
	 * 添加日程事件
	 * @throws UnsupportedEncodingException
	 */
	public void add() throws UnsupportedEncodingException{
		PrintWriter out=null;
		calbiz.save(calendar);
		//添加操作日志
		OperatelogBizImpl.add("添加", "添加日程事件");
		//---------------向schedule表中添加相应的内容----------------------------------------
		Userinfo uf=(Userinfo) session.get("userinfo");
		int ifprivate=1;
		//得到用户所在部门
		Departinfo departinfo=(Departinfo) session.get("depart");
		
		System.out.println("departinfo输出的内容"+departinfo.getDepartname());
		Schedule schedule=new Schedule(departinfo, uf, calendar , ifprivate);
		sbiz.save(schedule);
		//----------------添加动作结束------------------------------------------------------
		try {
			out = response.getWriter();
			/*out.print("<script>alert('添加成功！')</script>");*/
			out.print("<script>window.location.href='../admin_cg/date_yz/index.jsp"
					+ "'</script>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	/**
	 * 得到点击日程的所有信息
	 * @throws IOException
	 */
	public void updateone() throws IOException{
		//将可能被操作的对象id写入session，若接下来的动作涉及到更新和删除不需从页面传递id
		session.put("mycalendarid", id);
		calendar=(Calendar) calbiz.getById(Calendar.class, id);
		System.out.println(calendar.getColor()+"\t"+calendar.getStart()+"\t"+calendar.getEnd()+"\t"+calendar.getTitle());
		session.put("icalendar", calendar);
		PrintWriter out=null;
		try {
			out = response.getWriter();
			out.print("<script>window.location.href='CalendarUpdate.jsp"
					+ "'</script>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	/**
	 * 更新日程
	 */
	public void update(){
		PrintWriter out=null;
		//添加操作日志
		OperatelogBizImpl.add("修改", "修改日程事件");
		calbiz.update(calendar);
		System.out.println("update执行完毕");
		try {
			out = response.getWriter();
			/*out.print("<script>alert('修改成功！')</script>");*/
			out.print("<script>window.location.href='../admin_cg/date_yz/index.jsp"
					+ "'</script>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	
	/**
	 * 删除日程
	 */
	public void delete(){
		//添加操作日志
		OperatelogBizImpl.add("删除", "删除日程事件");
		id=(int) session.get("mycalendarid");
		System.out.println("id="+id);
		PrintWriter out=null;
		//---------------获取到所有引用该日历id的日程事件--------------------------------
		List<Schedule> s=sbiz.getAllByHQL("from Schedule s where s.calendar="+id);
		if(s.size()>0){
			for (Schedule schedule : s) {
				sbiz.delete(schedule);
			}
		}
		//---------------删除引用外键结束-----------------------------------------------
		calbiz.delete(calendar);//删除选择删除的日程
		System.out.println("delete执行完毕");
		try {
			out = response.getWriter();
			out.print("<script>alert('删除成功！')</script>");
			out.print("<script>window.location.href='../admin_cg/date_yz/index.jsp"
					+ "'</script>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}

	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}

	public CalendarBiz getCalbiz() {
		return calbiz;
	}

	public void setCalbiz(CalendarBiz calbiz) {
		this.calbiz = calbiz;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}


	/*public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public int getAllday() {
		return allday;
	}

	public String getTitle() {
		return title;
	}

	public String getColor() {
		return color;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	public void setAllday(int allday) {
		this.allday = allday;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setColor(String color) {
		this.color = color;
	}

	*/
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
	}

	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response=arg0;
	}

	public DepartInfoBiz getDbiz() {
		return dbiz;
	}

	public void setDbiz(DepartInfoBiz dbiz) {
		this.dbiz = dbiz;
	}

	public ScheduleBiz getSbiz() {
		return sbiz;
	}

	public void setSbiz(ScheduleBiz sbiz) {
		this.sbiz = sbiz;
	}
	
	
}
